<?php
	/*申请退货*/
	include('../conn.php');
	require('../../tool/UploadImage.class.php');


	$result_data_array = array();
	$ordersid = $_REQUEST['ordersid'];

	$user_comment = $_REQUEST['user_comment'];
	

	if(!$ordersid){
		$result_data_array['code'] = -100;
		$result_data_array['msg'] = '订单信息错误';
		echo json_encode($result_data_array);
		exit;
	}

	if(!$user_comment){
		$result_data_array['code'] = -100;
		$result_data_array['msg'] = '请填写退货原因';
		echo json_encode($result_data_array);
		exit;

	}


// 查找订单信息 
	$query = mysql_query("select state from goodsorders where ordersid=".$ordersid);
	$goodsorders = mysql_fetch_assoc($query);
	if(!$goodsorders){
		$result_data_array['code'] = -100;
		$result_data_array['msg'] = '订单信息错误';
		echo json_encode($result_data_array);
		exit;
	}

	$orderstate = $goodsorders['state'];

	// 上传照片
	$user_images  = '';
	$user_srcimages = '';
	if ($_FILES['user_images']['name'][0] != '') {
		$upload = new Upload("user_images");
		$date = date('Y-m-d');
		$sumbimage = '../../sumbimage/'.$date;
		$srcimage = '../../srcimage/'.$date;
		$upload->setFilePath($srcimage);
		$upload->setSmallFilePath($sumbimage);
		$re = $upload->upload();
		if($re){
			$path = $upload->getPath();
			$smallPath = $upload->getSmallimg();
			
			foreach ($path as $key => $value) {
				$path[$key] = 'srcimage/'.$date.'/'.$value;
				$smallPath[$key] = 'sumbimage/'.$date.'/'.$smallPath[$key];
			}
		
			$user_srcimages = implode(',',$path);
			$user_images = implode(',',$smallPath);
		}

	}
	mysql_query("BEGIN");
	
	// 上传照片 end
	$query = mysql_query("INSERT INTO tuihuo (ordersid,orderstate,user_comment,user_images,user_srcimages) VALUES ('".$ordersid."',".$orderstate.",'".$user_comment."','".$user_images."','".$user_srcimages."')");

	$id = mysql_insert_id();


	
	if(!$id){
		mysql_query("ROLLBACK");  
		mysql_query("END");
		$result_data_array['code'] =  -100;
		$result_data_array['msg'] = '申请退货失败1';
		echo json_encode($result_data_array);
		exit;
	}


	//更新订单状态
	$update_order_query = mysql_query("UPDATE goodsorders set state = 4 where ordersid=".$ordersid);
	
	if(mysql_affected_rows() <= 0){
		mysql_query("ROLLBACK");  
		mysql_query("END");
		$result_data_array['code'] =  -100;
		$result_data_array['msg'] = '申请退货失败2';
		echo json_encode($result_data_array);
		exit;
	}

	mysql_query("COMMIT"); 
	// mysql_query("ROLLBACK");
	mysql_query("END"); 
	$result_data_array['code'] = 200;
	$result_data_array['msg'] = '申请退货成功';
	$result_data_array['id'] = $id;
	echo json_encode($result_data_array);
	//调用信息推送接口
	$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state4."&ordersid=".$ordersid;
	file_get_contents($url);
	// 调用信息推送接口 end



?>